package com.qihunet.springbootjwt.controller;

import com.qihunet.springbootjwt.config.JwtTokenUtil;
import com.qihunet.springbootjwt.config.JwtUser;
import com.qihunet.springbootjwt.exception.BusinessException;
import com.qihunet.springbootjwt.request.LoginRequest;
import com.qihunet.springbootjwt.response.BaseResponseData;
import com.qihunet.springbootjwt.response.LoginResponse;
import com.qihunet.springbootjwt.service.AuthenticationService;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Collections;

/**
 * @author gary
 */
@RestController
@RequestMapping(value="auth")
public class AuthenticationController extends BaseController{

    @Resource
    private AuthenticationService authenticationService;

    @RequestMapping(value="login", method=RequestMethod.POST)
    public ResponseEntity<BaseResponseData> login(@RequestBody LoginRequest loginRequest, HttpServletRequest request) throws AuthenticationException {
        LoginResponse response =  null;
        try{
            response = authenticationService.loginPlatform(request, loginRequest);
        }catch(BusinessException e){
            return fail(e.getErrorCode(), e.getErrorMsg(), e.getCause());
        }
        return success(response);
    }
}

